//
//  ViewController.m
//  whereami
//
//  Created by student on 16/4/1.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "Place.h"
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *longitude;
@property (weak, nonatomic) IBOutlet UILabel *latitude;
@property(nonatomic,strong) CLLocationManager *locationmanager;
@property (weak, nonatomic) IBOutlet MKMapView *mapview;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)b1:(id)sender
{
    self.locationmanager=[[CLLocationManager alloc]init];
    self.locationmanager.desiredAccuracy=kCLLocationAccuracyBest;
    self.locationmanager.delegate=self;
    self.locationmanager.distanceFilter=100.0f;
    [self.locationmanager requestWhenInUseAuthorization];
    [self.locationmanager startUpdatingHeading];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *newLocation=[locations lastObject];
    self.longitude.text=[NSString stringWithFormat:@"%.5f",newLocation.coordinate.longitude];
    self.latitude.text=[NSString stringWithFormat:@"%.5f",newLocation.coordinate.latitude];
    MKCoordinateSpan span={0.01,0.01};
    MKCoordinateRegion region={newLocation.coordinate,span};
    [self.mapview setRegion:region];
    CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *place=[placemarks lastObject];
        NSLog(@"%@",place.name);
        NSLog(@"%@",place.thoroughfare);
        NSLog(@"%@",place.subThoroughfare);
        NSLog(@"%@",place.subLocality);
        NSLog(@"%@",place.locality);
        NSLog(@"%@",place.country);
    }];
    
    Place *myplace=[[Place alloc]init];
    myplace.coordinate=newLocation.coordinate;
    myplace.title=@"workplace";
    myplace.subtitle=@"zhengzelu";
    self.mapview.delegate=self;
    [self.mapview addAnnotation:myplace];
    
    [self.locationmanager stopUpdatingLocation];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *pinview=[mapView dequeueReusableAnnotationViewWithIdentifier:@"Anntotaion"];
    if(pinview==nil)
    {
        pinview=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Anntotaion"];
        
    }
    pinview.pinTintColor=[UIColor blueColor];
    pinview.animatesDrop=YES;
    pinview.canShowCallout=YES;
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    btn.tag=101;
    [btn addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];
    pinview.rightCalloutAccessoryView=btn;
    return pinview;
    
    
}
-(void)haha:(UIButton *)btn
{
    [self performSegueWithIdentifier:@"goto" sender:nil];
}
@end
